﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BackupLibrary;
using DevExpress.XtraEditors;

namespace BackupJob
{
    public partial class NewJob : XtraForm
    {
        bool Kaydedildi = false;

        public NewJob()
        {
            InitializeComponent();
            cmbDatabase.GotFocus += new EventHandler(cmbDatabase_GotFocus);
            cmbDatabase2.GotFocus += new EventHandler(cmbDatabase_GotFocus);
        }

        void cmbDatabase_GotFocus(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(cmbSqlServer.Text))
                {
                    BackupLibrary.Job jb = new Job();
                    jb.Database = cmbDatabase.Text;
                    jb.Password = txtPassword.Text;
                    jb.Server = cmbSqlServer.Text;
                    jb.UserName = txtUserName.Text;

                    if (cmbSecurity.SelectedIndex == 0)
                    {
                        jb.Security = BackupLibrary.Job.AutenticationTypes.Sql;
                    }
                    else
                    {
                        jb.Security = BackupLibrary.Job.AutenticationTypes.Windows;
                    }

                    Database db = new Database();
                    foreach (string vt in db.GetDatabase(jb))
                    {
                        cmbDatabase.Properties.Items.Add(vt, false);
                        cmbDatabase2.Properties.Items.Add(vt);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Sunucuya bağlanılamadı !");
            }
        }

        private Jobs _jobs = new Jobs();
        public Jobs SavingJobs { get { return _jobs; } }
        public Job ChangingJob { get; set; }

        private void btnSqlList_Click(object sender, EventArgs e)
        {
            DataTable dt = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbSqlServer.Properties.Items.Add(dt.Rows[i]["ServerName"].ToString());
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtexpired.Text))
            {
                txtexpired.Text = "7";
            }

            Kaydedildi = true;
            if (cmbSqlServer.Text == "" || txtCopyLocalPath.Text == "")
            {
                MessageBox.Show("Lütfen Bütün Alanları Doldurunuz");
                return;
            }

            if (cmbSecurity.Text == "Sql")
            {
                if (txtUserName.Text == "")
                {
                    MessageBox.Show("Lütfen Kullanıcı Adı ve Parola Bilgilerini Doldurunuz");
                    return;
                }
            }

            if (cmbSendType.Text == "Ftp")
            {
                if (txtFtpAdress.Text == "" || txtFtpUserName.Text == "" || txtFtpPassword.Text == "")
                {
                    MessageBox.Show("Lütfen Ftp Adres, Kullanıcı ve Parola Bilgilerini Doldurunuz");
                    return;
                }
            }

            if (this.ChangingJob == null) //yeni eklenecek
            {
                for (int i = 0; i < cmbDatabase.Properties.Items.Count; i++)
                {
                    if (cmbDatabase.Properties.Items[i].CheckState == CheckState.Checked)
                    {
                        BackupLibrary.Job Jb = new Job();
                        Jb.Database = cmbDatabase.Properties.Items[i].Value.ToString();
                        Jb.doCompress = chkCompress.Checked;
                        Jb.Folder = txtCopyLocalPath.Text;
                        Jb.FtpPassword = txtFtpPassword.Text;
                        Jb.FtpPort = 21;
                        Jb.FtpServer = txtFtpAdress.Text;
                        Jb.FtpFolder = txtCopyLocalPath.Text;
                        Jb.FtpUser = txtFtpUserName.Text;
                        Jb.Password = txtPassword.Text;
                        Jb.Server = cmbSqlServer.Text;
                        Jb.StartingDate = dteStartDate.Value;
                        Jb.UserName = txtUserName.Text;
                        Jb.LastExecuteDate = DateTime.Now;

                        Jb.MaxExpireDays = Convert.ToInt32(txtexpired.Text);

                        if (cmbSecurity.SelectedIndex == 0)
                        {
                            Jb.Security = BackupLibrary.Job.AutenticationTypes.Sql;
                        }
                        else
                        {
                            Jb.Security = BackupLibrary.Job.AutenticationTypes.Windows;
                        }

                        if (cmbSendType.SelectedIndex == 0)
                        {
                            Jb.Type = BackupLibrary.Job.BackupTypes.Ftp;
                        }
                        else
                        {
                            Jb.Type = BackupLibrary.Job.BackupTypes.Folder;
                        }

                        this.SavingJobs.AllTask.Add(Jb);
                    }
                }
            }
            else //değiştir
            {
                BackupLibrary.Job Jb = this.ChangingJob;
                Jb.Database = cmbDatabase2.Text;
                Jb.doCompress = chkCompress.Checked;
                Jb.Folder = txtCopyLocalPath.Text;
                Jb.FtpPassword = txtFtpPassword.Text;
                Jb.FtpPort = 21;
                Jb.FtpServer = txtFtpAdress.Text;
                Jb.FtpFolder = txtCopyLocalPath.Text;
                Jb.FtpUser = txtFtpUserName.Text;
                Jb.Password = txtPassword.Text;
                Jb.Server = cmbSqlServer.Text;
                Jb.StartingDate = dteStartDate.Value;
                Jb.UserName = txtUserName.Text;
                Jb.LastExecuteDate = DateTime.Now;

                Jb.MaxExpireDays = Convert.ToInt32(txtexpired.Text);

                if (cmbSecurity.SelectedIndex == 0)
                {
                    Jb.Security = BackupLibrary.Job.AutenticationTypes.Sql;
                }
                else
                {
                    Jb.Security = BackupLibrary.Job.AutenticationTypes.Windows;
                }

                if (cmbSendType.SelectedIndex == 0)
                {
                    Jb.Type = BackupLibrary.Job.BackupTypes.Ftp;
                }
                else
                {
                    Jb.Type = BackupLibrary.Job.BackupTypes.Folder;
                }
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void NewJob_Load(object sender, EventArgs e)
        {

            cmbSendType.Properties.Items.Add("Ftp");
            cmbSendType.Properties.Items.Add("Dizin");

            cmbSecurity.Properties.Items.Add("Sql");
            cmbSecurity.Properties.Items.Add("Windows");

            cmbSecurity.SelectedIndex = 1;
            cmbSendType.SelectedIndex = 1;

            if (this.ChangingJob != null)
            {
                cmbDatabase2.Visible = true;
                cmbDatabase.Visible = false;
                cmbDatabase2.Text = this.ChangingJob.Database;
                chkCompress.Checked = this.ChangingJob.doCompress;
                txtCopyLocalPath.Text = this.ChangingJob.Folder;
                txtFtpPassword.Text = this.ChangingJob.FtpPassword;
                txtFtpAdress.Text = this.ChangingJob.FtpServer;
                txtCopyLocalPath.Text = this.ChangingJob.FtpFolder;
                txtFtpUserName.Text = this.ChangingJob.FtpUser;
                txtPassword.Text = this.ChangingJob.Password;
                cmbSqlServer.Text = this.ChangingJob.Server;
                dteStartDate.Value = this.ChangingJob.StartingDate;
                txtUserName.Text = this.ChangingJob.UserName;
                txtexpired.Text = this.ChangingJob.MaxExpireDays.ToString();

                if (this.ChangingJob.Security == BackupLibrary.Job.AutenticationTypes.Sql)
                {
                    cmbSecurity.SelectedIndex = 0;
                }
                else
                {
                    cmbSecurity.SelectedIndex = 1;
                }

                if (this.ChangingJob.Type == BackupLibrary.Job.BackupTypes.Ftp)
                {
                    cmbSendType.SelectedIndex = 0;
                }
                else
                {
                    cmbSendType.SelectedIndex = 1;
                }
            }
            else
            {
                cmbDatabase2.Visible = false;
                cmbDatabase.Visible = true;
            }
        }

        private void cmbSecurity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSecurity.SelectedIndex == 0)
            {
                txtUserName.Enabled = true;
                txtPassword.Enabled = true;
            }
            else
            {
                txtUserName.Enabled = false;
                txtPassword.Enabled = false;
            }
        }

        private void cmbSendType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSendType.SelectedIndex == 0)
            {
                txtFtpAdress.Enabled = true;
                txtFtpPassword.Enabled = true;
                txtFtpUserName.Enabled = true;
                button3.Visible = false;
                label10.Text = "           Ftp Dizini :";
            }
            else
            {
                button3.Visible = true;
                txtFtpAdress.Enabled = false;
                txtFtpPassword.Enabled = false;
                txtFtpUserName.Enabled = false;
                label10.Text = "Yedekleme Dizini :";
            }
        }

        private void dteStartDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void NewJob_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!Kaydedildi)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Kaydedildi = true;
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            this.Close();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
